package com.cedifie.lendapp.utils.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.os.CountDownTimer
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.cedifie.lendapp.MyApplication
import com.cedifie.lendapp.R
import com.cedifie.lendapp.databinding.DialogClearDataBinding
import com.cedifie.lendapp.http.HttpHelper
import com.cedifie.lendapp.utils.event.CedEventType
import com.lib.base.utils.DensityUtils

/**
 * Cedifie
 * @Author wwdeng
 * @CreateTime: 2024/6/14 15:50
 * @Annotation：清除用户数据dialog
 */
class ClearDataDialog: ProDialog {

    private lateinit var binding: DialogClearDataBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    //等待时长
    private var countdownTimer: MyCountDownTimer? = null
    private var countdown = 10

    override fun initWindow() {
        val params = window?.attributes
        params?.width = MyApplication.app.getPhoneWidth() - DensityUtils.dp2px(60f)
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_scale_anim) //修改动画样式
        window?.setGravity(Gravity.CENTER)
    }

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context?, remark: String, callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogClearDataBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {

    }

    override fun initListener() {
        setOnClickListener(R.id.ivClose) //关闭
        setOnClickListener(R.id.btnReturn)
        setOnClickListener(R.id.btnConfirm)
    }

    override fun initData() {
        startCountdownTimer()
    }

    override fun onViewClick(v: View?) {
        dismiss()
        if (v?.id == R.id.btnConfirm){
            if(mCallback != null){
                mCallback?.onClick(v)
            }
        }
    }

    class MyCountDownTimer(private val dialog: ClearDataDialog, millisInFuture: Int, countDownInterval: Long): CountDownTimer(millisInFuture.toLong(), countDownInterval) {
        override fun onTick(millisUntilFinished: Long) {
            // 这里每隔一段时间（定时器间隔）会被调用
            // 可以在这里更新UI，显示剩余时间
            dialog.countdown = (millisUntilFinished / 1000).toInt()
            dialog.checkClearUserDataStatus()
        }
        override fun onFinish() {
            // 倒计时完成时被调用
            dialog.checkClearUserDataStatus()
        }
    }

    /**
     * 启动倒计时
     */
    @SuppressLint("SetTextI18n")
    private fun startCountdownTimer() {
        //随机 60秒 等待时长
        countdown = 10
        checkClearUserDataStatus()
        if (countdownTimer == null){
            countdownTimer = MyCountDownTimer(this, countdown * 1000, 1000)
        }
        countdownTimer?.start()
    }

    @SuppressLint("SetTextI18n")
    private fun checkClearUserDataStatus(){
        if (countdown <= 0){
            binding.btnConfirm.isEnabled = true
            binding.btnConfirm.text = context.getString(R.string.btn_confirm)
        } else {
            binding.btnConfirm.isEnabled = false
            binding.btnConfirm.text = "${context.getString(R.string.btn_confirm)}(${countdown}s)"
        }
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addCedEventAction(CedEventType.DIALOG.CED_DIALOG_USER_DATA_CLEAR_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addCedEventAction(CedEventType.DIALOG.CED_DIALOG_USER_DATA_CLEAR_DISMISS,eventRemark)
    }
}